package com.example.rdcproject.ving.notes.dao;

import com.example.rdcproject.ving.notes.entity.Announcement;
import com.example.rdcproject.ving.notes.service.ConnectionPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;


public class AnnouncementBoardDao {

    private Connection connection;
    private ConnectionPool connectionPool = ConnectionPool.getInstance();
    private ResultSet rs;


    public  AnnouncementBoardDao() {
        Announcement announcement = new Announcement();
        try {
            connection = connectionPool.getConnection();
            String sql = "select * from announcementboard order by publishTime desc limit 1";
            PreparedStatement statement = connection.prepareStatement(sql);

            rs = statement.executeQuery();

            while (rs.next()) {
                String title = rs.getString("title");
                String content = rs.getString("content");
                LocalDateTime dateTime = rs.getObject("publishTime", LocalDateTime.class);

                System.out.println("公告："+title+"\n"+"内容："+content+"\n"+"发布时间："+dateTime+"\n");
            }


            connectionPool.releaseConnection(connection);
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
